﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.OleDb;

public partial class nst_RssSitesEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitData();
        }
    }
    private void InitData()
    {   //ExpiryTime, 
        string siteId = Request["id"];
        int id;
        if(string.IsNullOrEmpty(siteId) || !int.TryParse(siteId, out id))
        {
            return;
        }
        string sql = "select SiteId, SiteName, IsValid, Url, NewsCount, TimeSpan, ErrorN, haveContent, haveImg, newsType, xmlSiteId, xmlFileName " +
            " from RssSites where SiteId=@SiteId";
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Parameters.Add("@SiteId", OleDbType.Integer).Value = siteId;
        OleDbDataReader reader = null;
        string errorN = "3";
        string type = string.Empty;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                LabelSiteId.Text = reader["SiteId"].ToString();
                txtNewsCount.Text = reader["NewsCount"].ToString();
                txtSiteName.Text = reader["SiteName"].ToString();
                txtTimeSpan.Text = reader["TimeSpan"].ToString();
                txtUrl.Text = reader["Url"].ToString();
                txtXmlFileName.Text = reader["xmlFileName"].ToString();
                txtXmlSiteId.Text = reader["xmlSiteId"].ToString();
                CheckBoxHaveContent.Checked = reader["haveContent"].ToString() == "1";
                CheckBoxHaveImg.Checked = reader["haveImg"].ToString() == "1";
                CheckBoxIsValid.Checked = reader["IsValid"].ToString() == "1";
                errorN = reader["ErrorN"].ToString();
                type = reader["newsType"].ToString();

                SetSelectItem(ddlErrorN, errorN);
                SetSelectItem(ddlType, type);
            }
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            AccessHelper.CloseDbResource(con, reader);
        }
    }
    private void SetSelectItem(DropDownList ddl, string value)
    {
        foreach(ListItem item in ddl.Items)
        {
            if(item.Value == value)
            {
                item.Selected = true;
                return;
            }
        }
    }
    protected void ButtonEdit_Click(object sender, EventArgs e)
    {
        string sql = "update RssSites set SiteId=@SiteId, SiteName=@SiteName, IsValid=@IsValid, Url=@Url, NewsCount=@NewsCount," +
            "TimeSpan=@TimeSpan, ErrorN=@ErrorN, haveContent=@haveContent, haveImg=@haveImg, newsType=@newsType, xmlSiteId=@xmlSiteId, xmlFileName=@xmlFileName " +
            " where SiteId=@SiteId";//ExpiryTime=@ExpiryTime, 
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Parameters.Add("@SiteId", OleDbType.Integer).Value = LabelSiteId.Text;
        cmd.Parameters.Add("@SiteName", OleDbType.VarWChar, 20).Value = txtSiteName.Text;
        cmd.Parameters.Add("@IsValid", OleDbType.Integer).Value = CheckBoxIsValid.Checked ? 1 : 0;
        cmd.Parameters.Add("@Url", OleDbType.VarChar, 200).Value = txtUrl.Text;
        cmd.Parameters.Add("@NewsCount", OleDbType.Integer).Value = txtNewsCount.Text;
        //cmd.Parameters.Add("@ExpiryTime", OleDbType.DateTime).Value = DateTime.Now;
        cmd.Parameters.Add("@TimeSpan", OleDbType.Integer).Value = txtTimeSpan.Text;
        cmd.Parameters.Add("@ErrorN", OleDbType.Integer).Value = ddlErrorN.SelectedValue;
        cmd.Parameters.Add("@haveContent", OleDbType.Integer).Value = CheckBoxHaveContent.Checked ? 1 : 0;
        cmd.Parameters.Add("@haveImg", OleDbType.Integer).Value = CheckBoxHaveImg.Checked ? 1 : 0;
        cmd.Parameters.Add("@newsType", OleDbType.Integer).Value = ddlType.SelectedValue;
        cmd.Parameters.Add("@xmlSiteId", OleDbType.VarChar, 20).Value = txtXmlSiteId.Text;
        cmd.Parameters.Add("@xmlFileName", OleDbType.VarChar, 30).Value = txtXmlFileName.Text;
        
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            LabelMsg.Text = "修改成功";
            txtUrl.Text = "";
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            AccessHelper.CloseDbResource(con, null);
        }
    }
}
